import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/server/http/request_header.dart';
import 'package:sports_new/utils/app_config.dart';
import 'package:sports_new/utils/constant.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/toast_utils.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/view/my/widgets/my_appbar.dart';
import 'package:sports_new/view/my/widgets/my_cellbox.dart';
import 'package:sports_new/widgets/base/qkd_button.dart';
import 'package:url_launcher/url_launcher_string.dart';

import '../../widgets/base/qkd_text.dart';

class MyAboutUsPage extends StatefulWidget {
  const MyAboutUsPage({super.key});

  @override
  State<MyAboutUsPage> createState() => _MyAboutUsPageState();
}

class _MyAboutUsPageState extends State<MyAboutUsPage> {
  String appName = "";
  String version = "";
  String channel = "";
  int bottomTapNum = 0;
  double firstTap = 0;

  @override
  void initState() {
    PackageInfo.fromPlatform().then((info) {
      appName = info.appName;
      version = "版本:${info.version}";
      update();
    });
    channel = AppConfig.config.channel ?? "";
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.scaffoldBg,
      appBar: const MyAppBar(title: "关于我们"),
      body: Stack(
        children: [
          Column(
            children: [
              const SizedBox(height: 8),
              header(),
              const SizedBox(height: 8),
              cellBox([
                cell("服务协议").tap(() {
                  Get.toNamed(Routes.webview,
                      arguments: {"url": Constant.serviceAgreementUrl});
                }),
                const Divider(
                    height: 0.5,
                    color: Colours.greyEE,
                    indent: 12,
                    endIndent: 12),
                cell("隐私政策").tap(() {
                  Get.toNamed(Routes.webview,
                      arguments: {"url": Constant.privacyPolicyUrl});
                }),
                const Divider(
                    height: 0.5,
                    color: Colours.greyEE,
                    indent: 12,
                    endIndent: 12),
                cell("用户充值协议").tap(() {
                  Get.toNamed(Routes.webview,
                      arguments: {"url": Constant.payPolicyUrl});
                }),
                const Divider(
                    height: 0.5,
                    color: Colours.greyEE,
                    indent: 12,
                    endIndent: 12),
                cell("自媒体服务用户协议").tap(() {
                  Get.toNamed(Routes.webview,
                      arguments: {"url": Constant.selfmediaPolicyUrl});
                }),
                const Divider(
                    height: 0.5,
                    color: Colours.greyEE,
                    indent: 12,
                    endIndent: 12),
                cell("未成年协议隐私保护协议").tap(() {
                  Get.toNamed(Routes.webview,
                      arguments: {"url": Constant.less18PolicyUrl});
                }),
                const Divider(
                    height: 0.5,
                    color: Colours.greyEE,
                    indent: 12,
                    endIndent: 12),
                cell("第三方SDK共享个人信息清单").tap(() {
                  Get.toNamed(Routes.webview,
                      arguments: {"url": Constant.thirdShareUrl});
                }),
              ]).marginSymmetric(horizontal: 8),
              const QkdText(
                "APP备案编号：桂ICP备2023005192号-6A",
                fontSize: 12,
                color: Colours.grey99,
              ).paddingOnly(top: 32),
              RichText(
                  text: TextSpan(
                      style:
                          const TextStyle(fontSize: 12, color: Colours.grey99),
                      children: [
                    const TextSpan(text: "查询链接："),
                    TextSpan(
                        text: "https://beian.miit.gov.cn/",
                        style: const TextStyle(color: Color(0xFF5DBBEA)),
                        recognizer: TapGestureRecognizer()
                          ..onTap = () => launchUrlString(
                              "https://beian.miit.gov.cn/",
                              mode: LaunchMode.externalApplication))
                  ]))
            ],
          ),
          Positioned(
              left: 0,
              bottom: 0,
              child: SizedBox(
                width: 60,
                height: 60,
                child: QkdButton(
                  onPressed: () {
                    onTap();
                  },
                ),
              ))
        ],
      ),
    );
  }

  Widget header() => Styles.card(
          child: Container(
        height: 199,
        width: double.infinity,
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(8)),
        child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
          SizedBox(
              width: 80,
              height: 80,
              child: Image.asset(Utils.getImgPath("qxb_logo.png"))),
          const SizedBox(height: 12),
          QkdText(appName, fontSize: 20, fontWeight: FontWeight.w600),
          const SizedBox(height: 2),
          QkdText(version + (channel.isNotEmpty ? " ($channel)" : ''),
              color: Colours.grey99, fontSize: 12)
        ]),
      )).marginSymmetric(horizontal: 8);

  Widget cellBox(List<Widget> children) => MyCellBox(children: children);
  // Container(
  //   clipBehavior: Clip.hardEdge,
  //   margin: const EdgeInsets.symmetric(horizontal: 8),
  //   decoration: BoxDecoration(
  //     color: Colours.white,
  //     borderRadius: BorderRadius.circular(8)
  //   ),
  //   child: Column(
  //     children: children,
  //   ),
  // );

  Widget cell(String text, {Widget? right}) => Container(
        padding: const EdgeInsets.symmetric(horizontal: 12),
        height: 52,
        child: Row(
          children: [
            QkdText(text, fontSize: 16),
            const Spacer(),
            if (right != null) right,
            if (right == null)
              Image.asset(Utils.getImgPath("icon_arrow_right.png"),
                  width: 6, height: 10)
          ],
        ),
      );

  void onTap() {
    bottomTapNum++;
    final currentTime =
        DateTime.now().millisecondsSinceEpoch.toDouble() / 1000.0;

    if (bottomTapNum == 1) {
      firstTap = currentTime;
    } else if (bottomTapNum == 5) {
      if ((currentTime - firstTap) < 2) {
        // setState(() {
        //   bottomTapNum = 0;
        //   // Reset the first tap time to avoid triggering on next six taps.
        //   firstTap = 0.0;
        // });
        ToastUtils.show(RequestHeader.env);
      }
    }
  }
}
